﻿
using System;
using System.Collections.Generic;
using WeDonekRpc.Client.Interface;
using WeDonekRpc.Client.Log;
using WeDonekRpc.Client.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.Log;
using WeDonekRpc.Model;

namespace WeDonekRpc.Client
{
    internal class RpcService : IService
    {
        public static IService Service = new RpcService();
        public event ReceiveMsgEvent ReceiveMsg;



        public event LoadExtend LoadExtend;
        public event Action StartUpComplate;
        public event Action StartUpComplating;

        public event SendIng SendIng;
        public event SendEnd SendComplate;
        public event ReceiveEndEvent ReceiveEnd;
        public event Action InitComplating;

        public event Action BeginIniting;
        public event Action Closing;

        public event ServerNodeStateChange RemoteState;
        public event NoServerEvent NoServerEvent;

        public void RemoteStateChange ( IRemoteRootNode remote, UsableState oldState, UsableState state )
        {
            this.RemoteState?.Invoke(remote, oldState, state);
            if ( LogSystem.CheckIsRecord(LogGrade.WARN) )
            {
                string title = string.Format("服务节点({0})状态从{1}变更为{2}!", remote.ServerName, oldState.ToString(), state.ToString());
                string show = string.Format("ip:{0}:{1}, serverId:{2}, sysType:{3}, IsUsable:{4}。", remote.ServerIp, remote.Port, remote.ServerId, remote.SystemType, remote.IsUsable ? "可用" : "不可用");
                RpcLogSystem.AddWarnLog(title, show);
            }
        }
        public void NoServerErrorEvent ( IRemoteConfig config, string sysType, object model )
        {
            this.NoServerEvent?.Invoke(config, sysType, model);
        }
        public void SendEvent ( ref SendBody send, int sendNum )
        {
            this.SendIng?.Invoke(ref send, sendNum);
        }

        public Dictionary<string, string> LoadExtendEvent ( string dictate )
        {
            if ( LoadExtend == null )
            {
                return null;
            }
            Dictionary<string, string> extend = [];
            LoadExtend(dictate, ref extend);
            if ( extend.Count == 0 )
            {
                return null;
            }
            return extend;
        }

        public void StartUp ()
        {
            this.StartUpComplate?.Invoke();
        }
        public void InitEvent ()
        {
            this.InitComplating?.Invoke();
        }
        public void BeginInit ()
        {
            this.BeginIniting?.Invoke();
        }
        public void StartUpIng ()
        {
            this.StartUpComplating?.Invoke();
        }
        public void ReceiveMsgEvent ( IMsg msg )
        {
            this.ReceiveMsg?.Invoke(msg);
        }

        public void ServiceClosing ()
        {
            this.Closing?.Invoke();
        }

        public void ReceiveEndEvent ( RemoteMsg msg, TcpRemoteReply reply )
        {
            this.ReceiveEnd?.Invoke(msg, reply);
        }

        public void SendEnd ( ref SendBody send, IRemoteResult result )
        {
            this.SendComplate?.Invoke(ref send, result);
        }
    }
}
